var entities = new Array();

// weapons
var shotgunImg = new Image();
shotgunImg.src = 'images/shotgun.png';

var rifleImg = new Image();
rifleImg.src = 'images/rifle.png';

// body
var helmImg = new Image();
helmImg.src = 'images/helm.png';

var shouldersImg = new Image();
shouldersImg.src = 'images/shoulders.png';

var leftArmImg = new Image();
leftArmImg.src = 'images/left_arm.png';

var rightArmImg = new Image();
rightArmImg.src = 'images/right_arm.png';

var playerDeadImg = new Image();
playerDeadImg.src = 'soldier_dead.png';

// sounds
var m4a1Single = new Audio("sounds/M4A1_Single.wav");

function Entity(name) {
    this.name = name;
    this.pos = new Vector();
    this.lastPos;
    this.lerpPos;
    this.time = 0;
    this.lastTime = 0;
    this.angle = 0;
    this.gameLogicState = "";
    this.moveState = "";
    this.hitpoints = "";
    this.rotateState = "";
    this.weaponAngleModifier = 0.0;
    this.weaponTransformationModifier = 0.0;

    this.fillUpWithJsonData = function(jsonData) {
        this.lastTime = this.time;
        this.time = new Date().getTime();
        this.lastPos = new Vector(this.pos.x, this.pos.y);
        this.pos.x = jsonData.x;
        this.pos.y = jsonData.y;

        var player = findEntity(playerId.value);
        if (player !== null && player.name !== jsonData.name) {
            this.angle = jsonData.angle;
        }
        this.gameLogicState = jsonData.gameLogicState;
        this.moveState = jsonData.moveState;
        this.hitpoints = jsonData.hitpoints;
        this.rotateState = jsonData.y;
    };

    this.updateLerpPos = function() {
        if (this.lastPos && this.lastPos.x && this.lastPos.y) {
            var passedTime = new Date().getTime() - this.time;
            var intervalTime = 50;
            var lerpFactor = passedTime / intervalTime;
            this.lerpPos = this.lastPos.mulS(1 - lerpFactor).add(this.pos.mulS(lerpFactor)).round();
        } else {
            this.lerpPos = this.pos;
        }
    }

    this.draw = function(context) {
        context.translate(this.lerpPos.x, this.lerpPos.y);
        if (this.gameLogicState === "ALIVE") {
            context.save();
            context.rotate(this.angle);

            context.save();
            context.rotate(0.6 + this.weaponTransformationModifier * 0.02);
            context.drawImage(leftArmImg, -50, -50);
            context.restore();

            context.save();
            context.rotate(0.1);
            context.translate(-this.weaponTransformationModifier, 0);
            context.drawImage(rightArmImg, -50, -50);
            context.restore();

            context.save();
            context.translate(-this.weaponTransformationModifier, 0);
            context.rotate(this.weaponAngleModifier);
            context.drawImage(rifleImg, -0, -40);
            context.restore();

            context.save();
            context.rotate(0.6);
            context.drawImage(shouldersImg, -50, -50);
            context.restore();

            context.drawImage(helmImg, -50, -50);
            context.restore();
        } else if (this.gameLogicState === "DEAD") {
            context.drawImage(playerDeadImg, -50, -50);
        } else if (this.gameLogicState === "OBSERVE") {
            //
        }

        /*
         var radius = 20;
         context.beginPath();
         context.arc(0, 0, radius, 0, 2 * Math.PI, false);
         context.fillStyle = 'green';
         context.fill();
         context.lineWidth = 1;
         context.strokeStyle = '#003300';
         context.stroke();
         */
    };

    this.update = function() {
        this.updateLerpPos();

        if (this.weaponAngleModifier !== 0) {
            if (this.weaponAngleModifier > -0.01 & this.weaponAngleModifier < 0.01)
            {
                this.weaponAngleModifier = 0;
            }
            else {
                if (this.weaponAngleModifier > 0) {
                    this.weaponAngleModifier = this.weaponAngleModifier - 0.01;
                }
                else {
                    this.weaponAngleModifier = this.weaponAngleModifier + 0.01;
                }
            }
        }
        if (this.weaponTransformationModifier > 0) {
            if (this.weaponTransformationModifier > 1.0) {
                this.weaponTransformationModifier = this.weaponTransformationModifier - 1.0;
            }
            else {
                this.weaponTransformationModifier = 0.0;
            }
        }
    };
}

function findAllEntities() {
    return entities;
}

function findEntity(name) {
    for (var i = 0; i < entities.length; i++) {
        if (entities[i].name === name) {
            return entities[i];
        }
    }
    return null;
}

function getFriendlyEntities() {
    return entities;
}

function updateEntity(jsonData) {
    for (var i = 0; i < entities.length; i++) {
        if (entities[i].name === jsonData.name) {
            entities[i].fillUpWithJsonData(jsonData);
            return;
        }
    }
    var entity = new Entity(jsonData.name);
    entity.fillUpWithJsonData(jsonData)
    entities[entities.length] = entity;
}

function removeEntity(entity) {
    for (var i = 0; i < entities.length; i++) {
        if (entities[i].name === entity.name) {
            entities.splice(i, 1);
        }
    }
}

function drawEntities(context) {
    for (var i = 0; i < entities.length; i++) {
        context.save();
        entities[i].draw(context);
        context.restore();
    }
}

function updateEntities() {
    for (var i = 0; i < entities.length; i++) {
        entities[i].update();
    }
}

function startShootAnimation(event) {
    var entity = findEntity(event.name);

    entity.weaponAngleModifier = event.weaponAngleModifier;
    entity.weaponTransformationModifier = 5;

    m4a1Single.currentTime = 0;
    m4a1Single.volume = 0.01;
    m4a1Single.play();
}